package com.example.josan.greendaodemo;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.NotNull;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.ToOne;
import org.greenrobot.greendao.annotation.Transient;
import org.greenrobot.greendao.annotation.Unique;
import org.greenrobot.greendao.DaoException;

/**
 * Created by wochacha on 2017/12/6.
 * 点击编译，就会自动生成setter和getter方法和构造方法
 * 至于构造方法的hash值是自动赋值上去的
 * 并且在该目录下生成DaoMaster和DaoSession
 * (其实不是在这个目录下生成的，而是在app/build/generated/greendao/包名/文件名下生成的)
 */

/**
 * @Entity 表明这个实体类会在数据库中生成一个与之对应的表
 */
@Entity
public class StudentMsgBean {
    // 对应数据表中的Id字，是一条数据的唯一标识
    @Id
    private Long id;

    // 表明这个属性对应数据表中的STUDENTNUM字段
    @Property(nameInDb = "STUDENTNUM")
    private String studentNum;

    // 可以自定义字段名，注意外键不能使用该属性
    @Property(nameInDb = "NAME")
    // 该属性值不能为空
    @NotNull
    // 数据库中只能有唯一值
    // 如果实体类的属性被unique注解，则传入的时候，不能插入两条该字段重复的记录，否则会报下面的错误
    // UNIQUE constraint failed: STUDENT_MSG_BEAN.NAME
    //@Unique
    private String name;

    // 该属性不会比存入数据库，也不会生成get/set方法，也不会出现在构造方法中
    @Transient
    private String notWrite;

    @Property(nameInDb = "SCORE_ID")
    private Long scoreId;
    
    @ToOne(joinProperty = "scoreId")
    private ScoreBean mScoreBean;

    /** Used to resolve relations */
    @Generated(hash = 2040040024)
    private transient DaoSession daoSession;

    /** Used for active entity operations. */
    @Generated(hash = 1576493926)
    private transient StudentMsgBeanDao myDao;

    @Generated(hash = 74897013)
    private transient Long mScoreBean__resolvedKey;

    @Generated(hash = 1095210225)
    public StudentMsgBean(Long id, String studentNum, @NotNull String name,
            Long scoreId) {
        this.id = id;
        this.studentNum = studentNum;
        this.name = name;
        this.scoreId = scoreId;
    }

    @Generated(hash = 160565988)
    public StudentMsgBean() {
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getStudentNum() {
        return this.studentNum;
    }

    public void setStudentNum(String studentNum) {
        this.studentNum = studentNum;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getScoreId() {
        return this.scoreId;
    }

    public void setScoreId(Long scoreId) {
        this.scoreId = scoreId;
    }

    /** To-one relationship, resolved on first access. */
    @Generated(hash = 2071419542)
    public ScoreBean getMScoreBean() {
        Long __key = this.scoreId;
        if (mScoreBean__resolvedKey == null
                || !mScoreBean__resolvedKey.equals(__key)) {
            final DaoSession daoSession = this.daoSession;
            if (daoSession == null) {
                throw new DaoException("Entity is detached from DAO context");
            }
            ScoreBeanDao targetDao = daoSession.getScoreBeanDao();
            ScoreBean mScoreBeanNew = targetDao.load(__key);
            synchronized (this) {
                mScoreBean = mScoreBeanNew;
                mScoreBean__resolvedKey = __key;
            }
        }
        return mScoreBean;
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 1313885936)
    public void setMScoreBean(ScoreBean mScoreBean) {
        synchronized (this) {
            this.mScoreBean = mScoreBean;
            scoreId = mScoreBean == null ? null : mScoreBean.getId();
            mScoreBean__resolvedKey = scoreId;
        }
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#delete(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 128553479)
    public void delete() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.delete(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#refresh(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 1942392019)
    public void refresh() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.refresh(this);
    }

    /**
     * Convenient call for {@link org.greenrobot.greendao.AbstractDao#update(Object)}.
     * Entity must attached to an entity context.
     */
    @Generated(hash = 713229351)
    public void update() {
        if (myDao == null) {
            throw new DaoException("Entity is detached from DAO context");
        }
        myDao.update(this);
    }

    /** called by internal mechanisms, do not call yourself. */
    @Generated(hash = 561829477)
    public void __setDaoSession(DaoSession daoSession) {
        this.daoSession = daoSession;
        myDao = daoSession != null ? daoSession.getStudentMsgBeanDao() : null;
    }
    
}
